package com.hd.common.page.impl;

import java.util.ArrayList;

import org.springframework.util.Assert;

import com.hd.common.page.Page;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 * <p>copyright:</p>
 * <p>Date: 2008-12-1</p>
 * @version V1.0
 * @amendment history:
 */
public class BasePage implements Page {

	public static final int DEFAULT_PAGE_SIZE = 15;
	
	protected Object elements;

	protected int pageSize;

	protected int pageNumber;

	protected int totalElementsCount = 0;
	
	public BasePage() {
		this(1,DEFAULT_PAGE_SIZE,0,new ArrayList<Object>(0));
	}
	 
	public BasePage(int pageNumber,int pageSize,int totalElementsCount,Object elements) {
		init(pageNumber,pageSize,totalElementsCount,elements);
	}
	
	protected void init(int pageNumber,int pageSize,int totalElementsCount) {
		init(pageNumber,pageSize,totalElementsCount,null);
	}
	
	protected void init(int pageNumber,int pageSize,int totalElementsCount,Object elements) {
		Assert.isTrue(pageSize > 0,"[pageSize] must great than zero");
		this.pageSize = pageSize;
		this.pageNumber = getPageNumber(pageNumber,pageSize,totalElementsCount);
		this.totalElementsCount = totalElementsCount;
		this.elements = elements;
	}
	
	public void setTotalNumberOfElements(int totalElementsCount) {
		this.totalElementsCount = totalElementsCount;
	}
	
	public boolean isFirstPage() {
		return getThisPageNumber() == 1;
	}

	public boolean isLastPage() {
		return getThisPageNumber() >= getLastPageNumber();
	}

	public boolean hasNextPage() {
		return getLastPageNumber() > getThisPageNumber();
	}

	public boolean hasPreviousPage() {
		return getThisPageNumber() > 1;
	}

	public int getLastPageNumber() {
		return getLastPageNumber(totalElementsCount, pageSize);
	}

	public Object getThisPageElements() {
		return elements;
	}

	public int getTotalNumberOfElements() {
		return totalElementsCount;
	}

	public int getThisPageFirstElementNumber() {
		return (getThisPageNumber() - 1) * getPageSize() + 1;
	}

	public int getThisPageLastElementNumber() {
		int fullPage = getThisPageFirstElementNumber() + getPageSize() - 1;
		return getTotalNumberOfElements() < fullPage ? getTotalNumberOfElements() : fullPage;
	}

	public int getNextPageNumber() {
		return getThisPageNumber() + 1;
	}

	public int getPreviousPageNumber() {
		return getThisPageNumber() - 1;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getThisPageNumber() {
		return pageNumber;
	}
	
    public static int getPageNumber(int pageNumber, int pageSize,int totalElements) {
		if(pageNumber <= 1) {
			return 1;
		}
    	if (Integer.MAX_VALUE == pageNumber
				|| pageNumber > getLastPageNumber(totalElements,pageSize)) { //last page
			return getLastPageNumber(totalElements,pageSize);
		}
		return pageNumber;
    }
    
	public static int getLastPageNumber(int totalElements,int pageSize) {
		int result = totalElements % pageSize == 0 ? 
				totalElements / pageSize 
				: totalElements / pageSize + 1;
		if(result < 1)
			result = 1;
		return result;
	}
    
}
